package com.tymoshenko.seabattle.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;
import com.tymoshenko.seabattle.client.ClientFactory;
import com.tymoshenko.seabattle.shared.model.domain.Point;

/**
 * @author Yakiv Tymoshenko
 * @since 29.01.13
 */
public class DashboardViewImpl extends Composite implements DashboardView {

    public static DashboardViewUiBinder viewUiBinder;
    @UiField
    FlexTable ownBoard;
    @UiField
    FlexTable opponentBoard;

    private NewGamePresenter newGamePresenter;
    private ShipPlacementPresenter shipPlacementPresenter;
    private ShootingPresenter shootingPresenter;
    private ClientFactory clientFactory;

    public DashboardViewImpl() {
        viewUiBinder = GWT.create(DashboardViewUiBinder.class);
        initWidget(viewUiBinder.createAndBindUi(this));
        repaintTable(ownBoard);
        repaintTable(opponentBoard);
    }

    @UiHandler("newGameButton")
    public void newGame(ClickEvent clickEvent) {
        newGamePresenter.startNewGame();
    }

    @UiHandler("opponentBoard")
    public void processShot(ClickEvent clickEvent) {
        Point target = Point.valueOf(opponentBoard.getCellForEvent(clickEvent).getRowIndex() + 1,
                opponentBoard.getCellForEvent(clickEvent).getCellIndex() + 1);
        shootingPresenter.shoot(target, clientFactory.getPlayer2());
    }

    public void repaintTable(FlexTable table) {
        table.clear();
        for (int row = 0; row < 10; ++row) {
            for (int col = 0; col < 10; ++col) {
                table.getCellFormatter().setStyleName(row, col, "gameBoardCell");
            }
        }
    }

    public void setNewGamePresenter(NewGamePresenter newGamePresenter) {
        this.newGamePresenter = newGamePresenter;
    }

    public ShipPlacementPresenter getShipPlacementPresenter() {
        return shipPlacementPresenter;
    }

    public void setShipPlacementPresenter(ShipPlacementPresenter presenter) {
        shipPlacementPresenter = presenter;
    }

    public ShootingPresenter getShootingPresenter() {
        return shootingPresenter;
    }

    public void setShootingPresenter(ShootingPresenter presenter) {
        shootingPresenter = presenter;
    }

    public FlexTable getOwnBoard() {
        return ownBoard;
    }

    public FlexTable getOpponentBoard() {
        return opponentBoard;
    }

    public void setClientFactory(ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
    }

    public interface DashboardViewUiBinder extends UiBinder<Widget, DashboardViewImpl> {
    }
}
